import socket
import select

ip = "localhost"
port = 6677
with socket.create_server((ip, port)) as sock:
    rlist = [sock]
    while True:
        rfd, wfd, xfd = select.select(rlist, [], [])
        if rfd:
            for fd in rfd:
                if fd is sock:
                    conn, addr = sock.accept()
                    rlist.append(conn)
                else:
                    data = fd.recv(1024)
                    if len(data) == 0:
                        print("peer conn close:", conn.getpeername())
                        rlist.remove(fd)
                        fd.close()
                    else:
                        fd.send(data)
